/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/
 
package testing;
import lime2.*;
import lights.adapters.*;
import lights.interfaces.*;
import java.util.Random;

/**
 * An agent for testing atomicity of execution of weak reactions that install (other) local weak reactions; the agent
 * installs an initial weak once reaction which is triggered by the <code>String</code> template: when it fires, it 
 * removes (inp) a tuple matching <code>String</code> and it installs an identical (weak) reaction.
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class InstW implements Runnable, ReactionListener
{
 // identification number	
 private int id = new Random().nextInt(1000);		
		
 // private tuple-related data members
 private LimeTupleSpace lts = LimeTupleSpace.get("testing");	
 private ITuple strings = new Tuple().add(new Field().setType(String.class));
 private LimeTemplate template = new LimeTemplate(Location.HERE, Location.UNSPECIFIED, strings);
 
 // private int: keeps track of the number of weak reactions previously installed
 private int n = 0;
 
 /**
  * Run method.
  */
 public void run()
 {
  System.out.println("InstW " + id + ": started");	
  Reaction r = new Reaction(Reaction.ONCE, template, this);
  lts.addWeakReaction(new Reaction[] {r});
  System.out.println("InstW " + id + ": initial weak reaction installed (0)");     
  System.out.println("InstW " + id + ": dies");
 }	
 
 /**
  * Reactive method.
  * 
  * @param re ReactionEvent
  */
 public void reactsToOut(ReactionEvent re)
 {
  Integer m = new Integer(n);	
  System.out.println("InstW " + id + ": weak reaction fires (" + m + ")");	
  lts.inp(template);
  n++;
  Reaction r = new Reaction(Reaction.ONCE, template, this);
  lts.addWeakReaction(new Reaction[] {r});             
  System.out.println("InstW " + id + ": weak reaction ends (" + m + ")");
  System.out.println("InstW " + id + ": ----------------------------------------------> (" + m.intValue() + ") was " + (n == (m.intValue() + 1) ? "atomic" : "non atomic"));
 }

@Override
public void reactsToTake(ReactionEvent re) {
	// TODO Auto-generated method stub
	
}
 	
}
